package com.salton123.whiteboard.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.os.Build;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * User: wujinsheng1@yy.com
 * Date: 2020/5/27 16:04
 * ModifyTime: 16:04
 * Description:
 */
public class BaseBoardView extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "BaseBoardView";
    private SurfaceHolder mSurfaceHolder;
    private RenderThread renderThread;
    private final Object mObjectLock = new Object();

    public BaseBoardView(Context context) {
        super(context, null);
    }

    public BaseBoardView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseBoardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void initView() {
        mSurfaceHolder = this.getHolder();
        mSurfaceHolder.addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        this.renderThread = new RenderThread();
        this.renderThread.setPriority(Thread.MAX_PRIORITY);
        this.renderThread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        this.renderThread.renderDone();
    }

    private class RenderThread extends Thread {
        private volatile boolean isDone;    //是否绘制完成

        @Override
        public void run() {
            Log.i(TAG, "start render...");
            while (!isDone) {
                synchronized (mObjectLock) {
                    renderFrame();
                    try {
                        mObjectLock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        public void renderDone() {
            this.isDone = true;
            try {
                notifyRender();
                join(); //子线程卡住
                Log.i(TAG, "renderDone ...");
            } catch (Exception e) {
                e.printStackTrace();
                Log.e(TAG, e.toString());
            }
        }
    }

    /**
     * 通知线程去渲染
     */
    public void notifyRender() {
        synchronized (mObjectLock) {
            mObjectLock.notifyAll();
        }
    }

    /**
     * 高版本支持硬件加速
     */
    private Canvas getLockCanvas() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Log.d(TAG, "lockHardwareCanvas");
            return this.mSurfaceHolder.lockHardwareCanvas();
        } else {
            // Log.d(TAG, "lockCanvas");
            return this.mSurfaceHolder.lockCanvas();
        }
    }

    /**
     * 帧渲染工作
     */
    private void renderFrame() {
        Canvas lockCanvas = getLockCanvas();
        try {
            long renderStartTime = SystemClock.elapsedRealtime();
            if (lockCanvas != null) {
                drawBackground(lockCanvas);
                drawRecord(lockCanvas);
            }
            Log.d(TAG, "render cost :" + (SystemClock.elapsedRealtime() - renderStartTime) + " ms");
        } catch (Throwable throwable) {
            Log.e(TAG, throwable.toString());
        } finally {
            if (lockCanvas != null) {
                this.mSurfaceHolder.unlockCanvasAndPost(lockCanvas);
            }
        }
    }

    public void drawBackground(Canvas lockCanvas) {

    }

    public void drawRecord(Canvas canvas) {

    }
}
